import Cookies from 'js-cookie'

const TokenKey = 'Kexilo-Token'
const RefreshTokenKey = 'Kexilo-Refresh-Token'

/**
 * 获取token
 */
export function getToken(): string | undefined {
  return Cookies.get(TokenKey)
}

/**
 * 设置token
 */
export function setToken(token: string): void {
  Cookies.set(TokenKey, token, { expires: 1 }) // 1天过期
}

/**
 * 移除token
 */
export function removeToken(): void {
  Cookies.remove(TokenKey)
  Cookies.remove(RefreshTokenKey)
}

/**
 * 获取刷新token
 */
export function getRefreshToken(): string | undefined {
  return Cookies.get(RefreshTokenKey)
}

/**
 * 设置刷新token
 */
export function setRefreshToken(refreshToken: string): void {
  Cookies.set(RefreshTokenKey, refreshToken, { expires: 7 }) // 7天过期
}

/**
 * 检查是否已登录
 */
export function isLogin(): boolean {
  return !!getToken()
}

/**
 * 清除所有认证信息
 */
export function clearAuth(): void {
  removeToken()
  localStorage.removeItem('kexilo-user')
  sessionStorage.removeItem('kexilo-tags-view')
}
